<template>
  <div class="meedu-main-body">
    <div class="float-left mb-30">
      <el-button @click="$router.push({ name: 'ReplyCreate' })" type="primary"
        >新建自动回复</el-button
      >
      <el-button @click="$router.push({ name: 'MpWechatMenu' })" type="primary"
        >公众号菜单</el-button
      >
      <OptionsBar text="公众号配置" value="微信公众号"></OptionsBar>
    </div>
    <div class="float-left">
      <el-table
        :header-cell-style="{ background: '#f1f2f9' }"
        :data="messages"
        class="float-left"
      >
        <el-table-column prop="id" label="ID" width="120"> </el-table-column>
        <el-table-column label="匹配规则" width="500"
          ><template slot-scope="scope">
            <span v-if="scope.row.type == 'event'"
              >{{ scope.row.type }}@{{ scope.row.event_type }}:{{
                scope.row.event_key
              }}</span
            >
            <span v-else>{{ scope.row.type }}@{{ scope.row.rule }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="reply_content" label="回复内容">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="
                $router.push({
                  name: 'ReplyUpdate',
                  query: { id: scope.row.id },
                })
              "
              >编辑</el-link
            >
            <el-link
              class="ml-5"
              type="danger"
              @click="deleteMessages(scope.row.id)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="float-left mt-30 text-center">
        <el-pagination
          @size-change="paginationSize"
          @current-change="paginationPage"
          :current-page="pagination.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pagination.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import OptionsBar from "@/components/option-bar.vue";
export default {
  components: {
    OptionsBar,
  },
  data() {
    return {
      pageName: "wechat-list",
      pagination: {
        page: 1,
        size: 10,
      },
      total: 0,
      messages: [],
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  methods: {
    paginationReset() {
      this.pagination.page = 1;
      this.getData();
    },
    paginationSize(size) {
      this.pagination.size = size;
      this.getData();
    },
    paginationPage(page) {
      this.pagination.page = page;
      this.getData();
    },
    getData() {
      this.$request
        .get(`mpWechatMessageReply`, {
          params: this.pagination,
        })
        .then((res) => {
          this.messages = res.data.data.data;
          this.total = res.data.data.total;
        });
    },
    //删除管理员
    deleteMessages(id) {
      this.$confirm("确认操作？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //点击确定按钮的操作
          this.$request
            .delete(`mpWechatMessageReply/${id}`)
            .then(() => {
              this.$message.success("删除成功");
              this.getData();
            })
            .catch((e) => {
              this.$message.error(e.message);
            });
        })
        .catch(() => {
          // 点×不提示
        });
    },
  },
  created() {
    this.getData();
  },
};
</script>
